﻿<%@ Page Title="" Language="VB" MasterPageFile="~/Views/Shared/Site.Master" Inherits="System.Web.Mvc.ViewPage" %>

<%@ Import Namespace="Equipment.Business" %>
<%@ Import Namespace="Equipment.DataAccess" %>
<asp:Content ID="Content1" ContentPlaceHolderID="TitleContent" runat="server">
    Edit
</asp:Content>
<asp:Content ID="Content4" ContentPlaceHolderID="CurrentMenuIDContent" runat="server">
    woMenu</asp:Content>
<asp:Content ID="Content3" ContentPlaceHolderID="HeadContent" runat="server">
    <link rel="stylesheet" type="text/css" href="../../Content/anytime.css" />
    <link rel="stylesheet" type="text/css" href="../../Content/jquery-ui-1.8.16.custom.css" />
    <script type="text/javascript" src="../../Scripts/jquery-ui-1.8.16.custom.min.js"></script>
    <script type="text/javascript" src="../../Scripts/anytime.js"></script>
    <script type="text/javascript" src="../../Scripts/jquery.tablesorter.min.js"></script>
    <script type="text/javascript" src="../../Scripts/jquery.tablesorter.pager.js"></script>
</asp:Content>
<asp:Content ID="Content2" ContentPlaceHolderID="MainContent" runat="server">
<% Dim currWO As WorkOrder = ViewData("CurrWorkOrder")%>
    <table>
        <tr>
            <td>
                <h2>
                    Edit Work Order</h2>
            </td>
            <td>
                <form id="print" method="post" target="_blank" action='<%= Url.Content("~/Report/WorkOrderDetailReport") %>'>
                <%= Html.Hidden("WorkOrder", currWO.ID)%>
                <input type="submit" class="button right" value="     Print     " />
                </form>
            </td>
        </tr>
    </table>
    <br />
    <% Using Html.BeginForm()%>
    <%= Html.ValidationSummary(False) %>
    <fieldset>
        <legend>Work Order</legend>
        <table class="struct-table">
            <tr>
                <th>
                    Work Order #:
                </th>
                <td>
                    <%: currWO.ID%>
                </td>
                <td>
                    &nbsp;
                </td>
                <th>
                    Asset Id:
                </th>
                <td>
                    <%: currWO.Asset.AssetID%>
                </td>
                <th>
                    Model #:
                </th>
                <td>
                    <%: currWO.Asset.ModelNo%>
                </td>
            </tr>
            <tr>
                <th>
                    Type of Work Order:
                </th>
                <td>
                    <%= Html.DropDownList("RequirementType")%>
                </td>
                <td>
                    &nbsp;
                </td>
                <th>
                    Asset Name:
                </th>
                <td>
                    <%: currWO.Asset.AssetName%>
                </td>
                <th>
                    Serial #:
                </th>
                <td>
                    <%: currWO.Asset.SerialNo%>
                </td>
            </tr>
            <tr>
                <th>
                    Due Date:
                </th>
                <td>
                    <%= Html.TextBox("txtDateDue", currWO.DateDue.ToString(Equipment.Core.Constants.UI.DATE_TIME_FORMAT), New With {.class = "text", .onkeydown = "return false;"})%>
                    <img class="ui-datepicker-trigger" src="../../Content/img/calendar.gif" alt="..."
                        title="..." onclick="$('#txtDateDue').click();" />
                    <%= Html.ValidationMessage("DateDue","*")%>
                </td>
                <td>
                    &nbsp;
                </td>
                <th>
                    Asset Type:
                </th>
                <td>
                    <%: currWO.Asset.AssetType.Description%>
                </td>
                <td>
                    &nbsp;
                </td>
                <td>
                    &nbsp;
                </td>
            </tr>
            <tr>
                <th>
                    Work Order Status:
                </th>
                <td>
                    <%: WorkOrderStatus.GetDescriptionByType(currWO.Status)%>
                </td>
                <td>
                    &nbsp;
                </td>
                <th>
                    Department:
                </th>
                <td>
                    <%: currWO.Asset.SiteDepartment.Description%>
                </td>
                <td>
                    &nbsp;
                </td>
                <td>
                    &nbsp;
                </td>
            </tr>
            <tr>
                <th>
                    Additional Details:
                </th>
                <td>
                    <%= Html.TextArea("txtDetails", currWO.Description, New With {.class = "normal-width"})%>
                </td>
                <td>
                    &nbsp;
                </td>
                <td>
                    &nbsp;
                </td>
                <td class="bottom">
                    <%= Html.DropDownList("ReviewType")%>
                </td>
                <td>
                    &nbsp;
                </td>
                <td>
                    &nbsp;
                </td>
            </tr>
            <tr>
                <th class="top">
                    Assigned To:
                </th>
                <td>
                    <div style="height: 300px; overflow: scroll;">
                        <% Html.RenderPartial("~/Views/Shared/Grids/FilteredGroupGrid.ascx", String.Empty)%>
                    </div>
                </td>
                <td>
                    &nbsp;
                </td>
                <th class="top">
                    Review By Group:
                </th>
                <td>
                    <div style="height: 300px; overflow: scroll;">
                        <% Html.RenderPartial("~/Views/Shared/Grids/FilteredGroupGrid.ascx", "Review")%>
                    </div>
                </td>
                <td>
                    &nbsp;
                </td>
                <td>
                    &nbsp;
                </td>
            </tr>
            <tr>
                <td colspan="7">
                    &nbsp;
                </td>
            </tr>
            <tr>
                <td colspan="6">
                    <h4>
                        Monitoring</h4>
                    <br />
                    <%If ViewData("Success_Mess") IsNot Nothing Then%>
                    <p id="status" class="success">
                        <%= ViewData("Success_Mess")%>
                    </p>
                    <%End If%>
                </td>
                <td>
                </td>
            </tr>
            <tr>
                <td colspan="7">
                    <table class="data-table">
                        <thead>
                            <tr>
                                <th>
                                    Measure
                                </th>
                                <th>
                                    Value
                                </th>
                                <th>
                                    Optimal
                                </th>
                                <th class="text-center">
                                    Additional
                                </th>
                                <th>
                                    &nbsp;
                                </th>
                            </tr>
                        </thead>
                        <tbody>
                            <% For Each measure As WorkOrderMeasure In currWO.Measures%>
                            <tr>
                                <td>
                                    <%= Html.Hidden(String.Format("measureType_{0}", measure.ID), measure.MeasureType.ToString())%>
                                    <%: measure.MeasureDescription%>
                                </td>
                                <%If measure.MeasureDescription = "Comments" Then%>
                                    <td colspan = "3">    
                                <%Else%>
                                    <td>
                                <% End If%>
                                    <% If measure.MeasureType = MeasureType.Text Or measure.MeasureType = MeasureType.Decimal Or measure.MeasureType = MeasureType.Integer Then%>
                                        <%If measure.MeasureDescription = "Comments" Then%>
                                            <%= Html.TextArea(String.Format("measure_{0}", measure.ID), measure.CalibratedValue, New With {.cols = "78", .rows = "5"})%>
                                        <%Else%>
                                            <%= Html.TextBox(String.Format("measure_{0}", measure.ID), measure.CalibratedValue, New With {.class = "text normal-width"})%>
                                        <%End If%>
                                    <%ElseIf measure.MeasureType = MeasureType.Checkbox Then%>
                                    <%  Dim checked As Boolean = False
                                        Dim checkedVal As String = measure.LookupValue.Split(";")(0)
                                        If checkedVal = measure.CalibratedValue Then
                                            checked = True
                                        End If
                                    %>
                                    <%= Html.CheckBox(String.Format("measure_{0}", measure.ID), checked)%>&nbsp;(<%:measure.LookupValue.Split(";")(0)%>/<%: measure.LookupValue.Split(";")(1)%>)
                                    <%ElseIf measure.MeasureType = MeasureType.Dropdown Then%>
                                    <%= Html.DropDownList(String.Format("measure_{0}", measure.ID), New SelectList(measure.LookupValue.Split(";"), measure.CalibratedValue))%>
                                    <%ElseIf measure.MeasureType = MeasureType.Radiobutton Then%>
                                    <% For Each item In measure.LookupValue.Split(";")%>
                                    <%= Html.RadioButton(String.Format("measure_{0}", measure.ID), item, measure.CalibratedValue = item)%>&nbsp;<%:item %>&nbsp;
                                    <%  Next
                                    End If%>
                                </td>
                                 <%If measure.MeasureDescription <> "Comments" Then%>
                                <td>
                                    <%If measure.MeasureType = MeasureType.Integer Then%>
                                    <%: measure.IntegerMin%>/<%: measure.IntegerMax%>
                                    <%= Html.Hidden(String.Format("measureOptMin_{0}", measure.ID), measure.IntegerMin)%>
                                    <%= Html.Hidden(String.Format("measureOptMax_{0}", measure.ID), measure.IntegerMax)%>
                                    <%ElseIf measure.MeasureType = MeasureType.Decimal Then%>
                                    <%: measure.DecimalMin%>/<%: measure.DecimalMax%>
                                    <%= Html.Hidden(String.Format("measureOptMin_{0}", measure.ID), measure.DecimalMin)%>
                                    <%= Html.Hidden(String.Format("measureOptMax_{0}", measure.ID), measure.DecimalMax)%>
                                    <%ElseIf measure.MeasureType = MeasureType.Dropdown Then%>
                                    <%: measure.OptimalLookupValue%>
                                    <%= Html.Hidden(String.Format("measureOptVal_{0}", measure.ID), measure.OptimalLookupValue)%>
                                    <%ElseIf measure.MeasureType = MeasureType.Radiobutton Then%>
                                    <%: measure.OptimalLookupValue%>
                                    <%= Html.Hidden(String.Format("measureOptVal_{0}", measure.ID), measure.OptimalLookupValue)%>
                                    <%ElseIf measure.MeasureType = MeasureType.Checkbox Then%>
                                    <%: measure.OptimalLookupValue%>
                                    <%= Html.Hidden(String.Format("measureOptVal_{0}", measure.ID), measure.OptimalLookupValue)%>
                                    <%= Html.Hidden(String.Format("measureOptOn_{0}", measure.ID), measure.LookupValue.Split(";")(0))%>
                                    <%= Html.Hidden(String.Format("measureOptOff_{0}", measure.ID), measure.LookupValue.Split(";")(1))%>
                                    <% End If%>
                                    &nbsp;
                                </td>
                                <td>
                                    <% If measure.RequiredComments > 0 Then%>
                                        <%= Html.TextBox(String.Format("measureComment_{0}", measure.ID), measure.MeasureComments, New With {.class = "text normal-width"})%>
                                    <% Else%>
                                        &nbsp;
                                    <%End If%>
                                </td>
                                <% End If%>
                                <td id="errPlace_<%: measure.ID%>" colspan = "3" width = "240px">
                                </td>
                            </tr>
                            <% Next%>
                            <tr>
                                <td colspan = "7">
                                    <span id = "sError" class = "field-validation-error left">
                                        &nbsp;
                                    </span>
                                    <p class="right">
                                        <input id = "bSave" type="submit" class="button middle-button" value="Save" onclick="if(validate() === false) return false;" />
                                    </p>
                                </td>
                            </tr>
                        </tbody>
                    </table>
                </td>
            </tr>
            <% If currWO.Status = WorkOrderStatusType.ReadyForReview Or currWO.Status = WorkOrderStatusType.ReadyForReviewWithExceptions Then%>
            <tr>
                <td colspan="6">
                        <h4>
                            Review Work Order Comments</h4>
                        <br />
                </td>
                <td>
                </td>
            </tr>
            <tr>
                <td  colspan="6">
                <%= Html.TextArea("txtReviewComments", currWO.ReviewComments, 7, 97, Nothing)%>
                </td>
                <td>
                </td>
            </tr>
            <% End If%>
        </table>
    </fieldset>
    <% Html.RenderPartial("~/Views/WorkOrder/Dialogs/WorkOrderAuthenticationDialog.ascx")%>
    <% End Using%>
    <div>
        <%: Html.ActionLink("Back to List", Session("BackURL"))%>&nbsp;
        <div id = "workorderedithistorybutton">
            <% Html.RenderPartial("~/Views/History/Controls/WorkOrderHistory.ascx")%>
        </div>
    </div>
    
    <script type="text/javascript">
            var regexpression = new RegExp("^[0-9]+(\.[0-9]{1,2})?$");
            var NOT_IN_RANGE_MESS = "<span class='error'>Value not in the optimal range.</span>";


            function validate() {
                var result = true;

                $('span.error').each(function(i) {
                    $(this).remove();
                });

                $("#sError").html("");
                $("#ReviewGroupContainer").removeClass("input-validation-error");
                $("#GroupContainer").removeClass("input-validation-error");
                if ($("#GroupContainer input:checkbox[checked]").length == 0){
                            $("#sError").append("<span id = 'emsg' class = 'field-validation-error'> Required Assigned Group</span><br>");
                            $("#GroupContainer").addClass("input-validation-error");
                            result = false;
                        }
                if ( $("#ReviewType").val() != "NotRequired"  ){
                        if ($("#ReviewGroupContainer input:checkbox[checked]").length == 0){
                            $("#sError").append("<span id = 'emsg' class = 'field-validation-error'> Required Review Group</span><br>");
                            $("#ReviewGroupContainer").addClass("input-validation-error");
                            result = false;
                        }
                }
                if(result === false)
                    return result;
                
                var meassIDs = [<%= String.Join(", ", currWO.Measures.Select(Function(m) m.ID))%>];
                
                for(i = 0; i < meassIDs.length; i++)
                {   
                    
                    var id = meassIDs[i];
                    var type = $("#measureType_"+id).val()

                    if(type === "5" || type === "6"){
                    
                        var elVal = $("#measure_"+id).val();
                        if (!elVal.match(regexpression)) {
                            $("#errPlace_" + id).html("<span class='error'>Invalid number.</span>");
                            result = false;
                        }
                        else{
                            var optMin = parseFloat($("#measureOptMin_" + id).val());
                            var optMax = parseFloat($("#measureOptMax_" + id).val());
                            var floatVal = parseFloat(elVal);
                            if (optMin > floatVal || floatVal > optMax) {
                                $("#errPlace_" + id).html(NOT_IN_RANGE_MESS);
                                result = false;
                            }
                        }
                    } else if(type === "1"){
                   
                        var elVal = $("#measure_" + id).val();
                        var optVal = $("#measureOptVal_" + id).val();
                        if (optVal != elVal) {
                            $("#errPlace_" + id).html(NOT_IN_RANGE_MESS);
                            result = false;
                        }

                    } else if(type === "3"){
                       
                        var elVal = $("#measure_" + id+":checked").val();
                        var optVal = $("#measureOptVal_" + id).val();
                        if (optVal != elVal) {
                             $("#errPlace_" + id).html(NOT_IN_RANGE_MESS);
                            result = false;
                        }
                    } else if(type === "2"){
                    
                        var isChecked = $("#measure_" + id).is(':checked');
                        var optVal = $("#measureOptVal_" + id).val();
                        var onValue = $("#measureOptOn_" + id).val();
                        var offValue = $("#measureOptOff_" + id).val();
                        var elVal = isChecked?onValue:offValue;
                        if (optVal != elVal) {
                             $("#errPlace_" + id).html(NOT_IN_RANGE_MESS);
                            result = false;
                        }
                    }
                }
                
                if (result === false) {
                    return confirm("Value not in the optimal range or misses. Press OK to confirm, cancel to enter new value.");
                }
                if (result) return AuthenticationForWorkOrder();
            }

            $(document).ready(function () {
                $('#txtDateDue').AnyTime_picker({ format: "%Y-%m-%d %h:%i %p"} );
                    $("select#DepartmentSource").change(function () {
                        loadDepartmentGroups();
                    });
                    $("select#ReviewDepartmentSource").change(function () {
                        loadReviewDepartmentGroups();
                    });
                    if (<%: currWO.Status%> == 5) ChangeAllToReadOnly();
            });

            function ChangeAllToReadOnly(){
                $("select, #bSave").attr("disabled","disabled");
                $("input:checkbox, input:radio").click(function() {return false;});
                $("input:text, #txtDetails").attr("readonly","readonly");
                $("#txtDateDue").AnyTime_noPicker();
              }

         function loadReviewDepartmentGroups(){
            var depID= $('#ReviewDepartmentSource').val();
            jQuery.ajax({
                type: 'POST',
                data: { depID: depID},
                url: '<%= Html.AttributeEncode(Url.Action("GetReviewGroupsByDepartmentID")) %>',
                success: function (result) {
                    $('#ReviewGroupContainer').html(result);
                }
            });
        }


        function loadDepartmentGroups(){
            var depID= $('#DepartmentSource').val();
            jQuery.ajax({
                type: 'POST',
                data: { depID: depID},
                url: '<%= Html.AttributeEncode(Url.Action("GetGroupsByDepartmentID")) %>',
                success: function (result) {
                    $('#GroupContainer').html(result);

                }
            });
        }
    </script>
</asp:Content>
